package com.gwsdk.paysdk.view.round;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Shader;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;

@SuppressLint("AppCompatCustomView")
public class CircleImageView extends ImageView {

	private Paint mPaint; //画笔

	private int mRadius; //圆形图片的半径

	private float mScale; //图片的缩放比例

	public CircleImageView(Context context) {
		super(context);
		clickcount = 0;
		isLongClick = false;
	}

	public CircleImageView(Context context, AttributeSet attrs) {
		super(context, attrs);
		clickcount = 0;
		isLongClick = false;
	}

	public CircleImageView(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		clickcount = 0;
		isLongClick = false;
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		//因为是圆形图片，所以应该让宽高保持一致
		int size = Math.min(getMeasuredWidth(), getMeasuredHeight());
		mRadius = size / 2;

		setMeasuredDimension(size, size);
	}

	@Override
	protected void onDraw(Canvas canvas) {

		mPaint = new Paint();
		if (getDrawable() == null) return;
		Bitmap bitmap = drawableToBitmap(getDrawable());

		//初始化BitmapShader，传入bitmap对象
		BitmapShader bitmapShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);

		//计算缩放比例
		mScale = (mRadius * 2.0f) / Math.min(bitmap.getHeight(), bitmap.getWidth());

		Matrix matrix = new Matrix();
		matrix.setScale(mScale, mScale);
		bitmapShader.setLocalMatrix(matrix);


		mPaint.setShader(bitmapShader);

		//画圆形，指定好中心点坐标、半径、画笔
		canvas.drawCircle(mRadius, mRadius, mRadius, mPaint);
	}

	//写一个drawble转BitMap的方法
	private Bitmap drawableToBitmap(Drawable drawable) {

		if (drawable instanceof BitmapDrawable) {
			BitmapDrawable bd = (BitmapDrawable) drawable;
			return bd.getBitmap();
		}
		int w = drawable.getIntrinsicWidth();
		int h = drawable.getIntrinsicHeight();
		Bitmap bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
		Canvas canvas = new Canvas(bitmap);
		drawable.setBounds(0, 0, w, h);
		drawable.draw(canvas);
		return bitmap;
	}

	private OnDoubleClickListener mListener;

	private volatile int clickcount;
	private volatile boolean isLongClick;

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		switch (event.getAction()){
			case MotionEvent.ACTION_DOWN:
				if(clickcount==0)
					handler.postDelayed(runnable,500);
				break;
			case MotionEvent.ACTION_UP:
				clickcount++;
				if(isLongClick){
					isLongClick=false;
					clickcount=0;
				}
				break;
		}
		return super.onTouchEvent(event);
	}
	public void setOnDoubleClick(OnDoubleClickListener listener){
		this.mListener = listener;
	}
	private Handler handler = new Handler(new Handler.Callback() {
		@Override
		public boolean handleMessage(Message msg) {
			if(mListener!=null&&msg.what>1){
				mListener.onDoubleClick(CircleImageView.this);
			}
			clickcount=0;
			return true;
		}
	});

	private Runnable runnable = new Runnable() {
		@Override
		public void run() {
			if(clickcount==0){
				isLongClick=true;
			}else {
				handler.sendEmptyMessage(clickcount);
			}
		}
	};

	public interface OnDoubleClickListener{
		void onDoubleClick(View view);
	}
}
